import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'

// 引用页面组件
import main from '../views/main/main'
import login from '../views/login/login'

// 使用router插件
Vue.use(Router)

// 从 store 中获取菜单列表
const menuList = store.state.menuStore.menuList

// 定义路由表
// 定义路由表
let routerMap = [
  {
    path: '/',
    code: '',
    name: '',
    component: login,
    redirect: '/login'
  },
  {
    path: '*',
    code: '',
    name: '',
    component: 404
  },
  {
    path: '/login',
    code: 'login',
    name: '登录',
    component: login
  },
  {
    path: '/main',
    code: 'main',
    name: '主页',
    component: main,
    children: menuList
    // redirect: '/count',
    // children: [
    //     {path: '/whHome', code: 'whHome', name: '首页', component: whHome, meta: {role: ['1'], nav: true}},
    //     {path: '/service', code: 'service', name: '工单维护', component: service, meta: {role: ['1'], nav: true}},
    //     {path: '/dept', code: 'dept', name: '处室维护', component: dept, meta: {role: ['1'], nav: true}},
    //     {path: '/emp', code: 'emp', name: '人员维护', component: emp, meta: {role: ['1'], nav: true}},
    // ]
  }
]

let route = new Router({
  // base: 'governance',
  // mode: 'history',
  routes: routerMap
})
route.beforeEach((to, from, next) => {
  //  设置路由跳转title名
  if (to.meta.title) {
    document.title = to.meta.title
  }
  if (to.path === '/login' || to.path === '/404' || to.path === '/redirect') {
    next()
  } else {
    let userInfo = JSON.parse(sessionStorage.getItem('GOVERNANCE_USER_INFO'))
    // 判断是否有 token，为空即没登录，跳转至登录页
    if (!userInfo || !userInfo.token) {
      if (process.env.NODE_ENV === 'development') {
        next('/login')
      } else {
        this.$router.push('/login')
      }
    } else {
      next()
    }
  }
})
export default route
